<? $page_title='供应商价格对比' ?>
<?include "FASHION/page_header.phtml"?>

<form action="<? url('procsum-do_elect') ?>" method="post" id="form1">
<div style="margin:3px auto; padding:3px; border:1px solid #cfcfcf;">
<b>供应商:</b>
<{foreach item=sp from=$all_suppliers}>
	<input type="checkbox" name="sspid[]" value="<{$sp.id}>" id="spid_<{$sp.id}>" 
		   <{if isset($suppliers[$sp.id]) }> checked<{/if}>
		/><label for="spid_<{$sp.id}>"><{$sp.name}></label> &nbsp;
<{/foreach}>
<input type="button" value="选择" onclick="
	var spids = [];
	$('input[name=\'sspid[]\']:checked').each(function(){spids.push($(this).val());});
	g2('<? url('procsum-elect/id/'.fetch_a_int('id').'/spids/SPIDS') ?>'.replace('SPIDS', spids.join(',')));
" class="button" />
</div>

<table class="list"><tbody>
<tr class="nobg">
	<th rowspan="2">材料编号</th>
	<th rowspan="2">材料名称</th>
	<th rowspan="2">品牌</th>
	<th rowspan="2">规格</th>
	<th rowspan="2">单位</th>
	<th rowspan="2">数量</th>
<{foreach item=s from=$suppliers}>
	<th colspan="2"><{$s.name}></th>
<{/foreach}>
	<th rowspan="2">供应商</th>
	<th rowspan="2">备注</th>
</tr>
<tr class="nobg">
<{foreach item=s from=$suppliers}>
	<th>单价</th>
	<th>金額</th>
<{/foreach}>
</tr>

<{foreach item=s from=$stuffs}>
<tr class='data<{if $s.supplier_id}> disabled<{/if}>'>
	<td class="nobr"><{$s.sno}></td>
	<td><{$s.name}></td>
	<td><{$s.brand}></td>
	<td><{$s.specific}></td>
	<td><{$s.unit}></td>
	<td class="decimal"><{$s.amount}></td>
        <?php 
              $arr = array();
              foreach($suppliers as $v)
              {
                  if($s['suppliers'][$v['id']]['quote']>0) $arr[]= $s['suppliers'][$v['id']]['quote'];
              }                          
          ?>
	<{foreach item=sp from=$suppliers}>                   
		<td class="decimal fnum sp_<{$sp.id}> price" sp="<{$sp.id}>" ware_id="<{$s.ware_id}>" <{if !$s.supplier_id && !empty($arr) && min($arr) && min($arr)==$s.suppliers[$sp.id].quote}>style="color:blue;"<{/if}> ><{$s.suppliers[$sp.id].quote }></td>
		<td class="decimal fnum sp_<{$sp.id}>" ware_id="<{$s.ware_id}>"><{= $s.suppliers[$sp.id].quote * $s.amount}></td>
	<{/foreach}>
	<{if $s.supplier_id}>
		<td><{$all_suppliers[$s.supplier_id].name}></td>
		<td><{$s.quote_remark}></td>
	<{else}>
		<td>
                    
                    <select name="spid[<{$s.ware_id}>]" ware_id="<{$s.ware_id}>">
                      <option value=""></option>                      
                      <{foreach item=sp from=$suppliers}>                      
                      <option value="<{$sp.id}>" <{if !empty($arr) && min($arr) && min($arr)==$s.suppliers[$sp.id].quote}> selected="selected"<{elseif $s.suppliers[$sp.id].quote==0}>disabled="disabled"<{/if}>><{$sp.name}></option>
                      <{/foreach}>                            
                    </select></td>
		<td><input type="text" name="remark[<{$s.ware_id}>]" id="remark_<{$s.ware_id}>" value="<{$s.quote_remark}>" class="rmk" size="8"/></td>
	<{/if}>
</tr>
<{/foreach}>
</table>
<div class="center">
	<INPUT TYPE="button" class="button" value="返回" onclick="location.href='<? url('procsum-quote') ?>'"> &nbsp;
	<INPUT TYPE="submit" class="button" value="保存比价結果" />
</div>
<input type="hidden" name="summary_id" value="<? fetch_a_int('id') ?>" />

</form>

<script>
$(function(){
	//$('select[name^=spid]').html('<option value=""></option><{foreach item=sp from=$suppliers}><option value="<{$sp.id}>"><{$sp.name}></option><{/foreach}>');
	$('select[name^=spid]').change(function(){
		$('td[ware_id='+$(this).attr('ware_id')+']').css('color', 'black');
		$('.sp_'+$(this).val()).filter('[ware_id='+$(this).attr('ware_id')+']').css('color', 'blue');
	});

	//toggle supplier filter
	//$('select[name^=spid]').each(function(){ 
		//default value | last value
		//if(parseInt($(this).attr('sp_id'))) $(this).val($(this).attr('val'));
	//	if($(this).val()) $('.sp_'+$(this).val()).filter('[ware_id='+$(this).attr('ware_id')+']').css('color', 'blue');
		//available supplier filter
//		var stfid	= $(this).attr('ware_id');
//		$(this.childNodes).each(function(){
//			if(this.tagName == 'OPTION'){
//				var prc	= $('.sp_'+$(this).val()).filter('[ware_id='+stfid+']');
//				if(prc.length && !parseFloat(prc.get(0).innerHTML)){
//					$(this).attr('disabled', true);
//				}
//			}
//		});
	//});

	//头部供应商选择
	var spids = '<?= fetch_a_string('spids') ?>'.split(',');
	for(var i=0;i<spids.length;i++){
		$('#spid_'+spids[i]).attr('checked', true);
	}
	
	ajax_form({id:'form1',before:function(){ return confirm('确认提交<?$page_title?>吗？');}});
	
	//自动选择低价供应商
//	$('.data').each(function(){
//		var child = $(this).find('.price');
//		var min = 2100000000;
//		var minobj = null;
//		for(i=0;i<child.length;i++){
//			var prc = parseFloat($(child[i]).html());
//			if(prc && min > prc){
//				min = prc;
//				minobj = child[i];
//			}
//		}
//		if(minobj){
//			$('select[ware_id='+$(minobj).attr('ware_id')+']').val( $(minobj).attr('sp') ).change();
//		}
//	});
});
</script>

<?include "FASHION/page_footer.phtml"?>
